#这个是要爬取中国天气预告网，并且进行数据的可视化

import requests

import html5lib
from pyecharts.charts import Bar
from bs4 import BeautifulSoup

basic_url='http://www.weather.com.cn/textFC/{}.shtml'
cities=[]

def parse(url):
    r=requests.get(url)
    r.encoding=r.apparent_encoding
    soup=BeautifulSoup(r.text,'html5lib')
    first_true=soup.select('.conMidtab')[0]
    divs=first_true.select('.conMidtab2')
    for div in divs:
        trs=div.select('tr')[2:]
        for index,tr in enumerate(trs):
            info=list(tr.stripped_strings)
            if index==0:
                city={'city':info[0],'template':int(info[5])}
            else:
                city={'city':info[0],'template':int(info[4])}
            cities.append(city)
def print_city():
    for city in cities:
        print('city:',city['city'],'template:',city['template'])

def geturl():
    url_list=['hb','db','hd','hz','hn','xb','xn']
    for i in url_list:
        url=basic_url.format(i)
        parse(url)
    cities.sort(key=lambda data:data['template'])
    data=cities[:10]
    char_city=list(map(lambda city:city['city'],data))
    char_temp=list(map(lambda city:city['template'],data))
    print(char_temp)
    chart=Bar()
    chart.add_xaxis(char_city)
    chart.add_yaxis('template',char_temp)
    chart.render('collection/weather.html')
geturl()
